package sren

import java.io.File
import javax.sound.midi.*

@main def DrumMain() = {
  val mds = MidiSystem.getMidiDeviceInfo
  mds.foreach(x => println(s"md: DeviceInfo: ${x.getName}, ${x.getDescription}") )

  val drum = MidiSystem.getMidiDevice(mds(3))
  val seqer = MidiSystem.getMidiDevice(mds(2))
  val loseq = MidiSystem.getSequencer

  val sef = MidiSystem.getSequence(new File("/home/work/test.mid"))

  loseq.setSequence(sef)

  drum.getTransmitter.setReceiver(new MidiHandler)
  //drum.getTransmitter.setReceiver(seqer.getReceiver)

  //seqer.open()
  drum.open()
}


class MidiHandler extends javax.sound.midi.Receiver {

  val loseq = MidiSystem.getSequencer
  loseq.open()
  //loseq.start()

  override def send(message: MidiMessage, timeStamp: Long): Unit = {
    val msg = message.getMessage
    println(s"send message: ${timeStamp}: ${message.getLength} ${message.getStatus} ${msg.mkString(" ")} ")
    loseq.getReceiver.send(message, timeStamp)
  }

  override def close(): Unit = {
    println("close ")

  }
}